import java.io.File;
import java.util.Scanner;

public class test{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入目录路径：");
        String directoryPath = scanner.nextLine();

        File directory = new File(directoryPath);

        if (directory.exists() && directory.isDirectory()) {
            long size = calculateDirectorySize(directory);
            System.out.println("目录 '" + directoryPath + "' 占用的空间大小为：" + size + " 字节");
        } else {
            System.out.println("输入的路径不存在或不是一个目录！");
        }

        scanner.close();
    }

    private static long calculateDirectorySize(File directory) {
        long size = 0;
        // 获取目录下的所有文件/文件夹
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                // 如果是文件，直接计算大小
                if (file.isFile()) {
                    size += file.length();
                } else { // 如果是目录，递归计算大小
                    size += calculateDirectorySize(file);
                }
            }
        }
        return size;
    }
}